package cn.wu.org.service.impl;

import cn.wu.org.basic.utils.PageList;
import cn.wu.org.domain.Department;
import cn.wu.org.mapper.DepartmentMapper;
import cn.wu.org.query.DepartmentQuery;
import cn.wu.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl implements IDepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;
    //根据id查询
    @Override
    public Department findById(Long id) {
        return departmentMapper.findById(id);
    }
    //普通的查询所有
    @Override
    public List<Department> findAll() {
        return departmentMapper.findAll();
    }
    //分页的查询所有
    @Override
    public PageList<Department> queryAll(DepartmentQuery query) {
        //去除高级查询输入时的空格
        if (!StringUtils.isEmpty(query.getKeyword())){
            query.setKeyword(query.getKeyword().trim());
        }
        //首先获取分页总条数
        Integer totals = departmentMapper.queryCount(query);

        //判断如果总条数为空，那么没有必要再查询数据
        if (totals == 0) {
            return new PageList<>();
        }
        //查询所有数据
        List<Department> departments = departmentMapper.queryAll(query);
        return new PageList<>(totals,departments);
    }
    //添加
    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
    }
    //删除
    @Override
    @Transactional
    public void delete(Long id) {
        departmentMapper.delete(id);
    }
    //修改
    @Override
    @Transactional
    public void update(Department department) {
        departmentMapper.update(department);
    }
}
